package com.example.webdownloadserver.base;

import com.example.webdownloadserver.core.excel.RowNum;
import com.example.webdownloadserver.util.ExcelUtil;
import com.example.webdownloadserver.vo.TaskCenter;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;



public abstract class TaskHandlerAbstract {
	
	
	/**
	 * 检查上传信息
	 * @param taskCenter
	 */
	protected void checkUpload(TaskCenter taskCenter) {
		//文件检查
        if(StringUtils.isEmpty(taskCenter.getTaskUpUrl())) {
        	throw new TaskJobException("上传文件地址为空");
        }
        if(StringUtils.isEmpty(taskCenter.getTaskFileName())) {
        	throw new TaskJobException("上传文件名为空");
        }
        if (!taskCenter.getTaskFileName().endsWith("xls") && !taskCenter.getTaskFileName().endsWith("xlsx") 
        		&& !taskCenter.getTaskFileName().endsWith("txt")) {
        	throw new TaskJobException("上传文件非xls、xlsx或txt格式");
        }
	}

	/**
	 *
	 * @param taskCenter 下载数据
	 * @param pojoClass 封装实体class
	 * @param skipRowNum 文件跳过skipRowNum行不读取
	 * @return
	 * @throws Exception
	 */
	protected <T> List<T> readExcelFile(TaskCenter taskCenter, Class<T> pojoClass,int skipRowNum) throws Exception {

		List<T> listResult = new ArrayList();
		Field[] declaredFields = pojoClass.getDeclaredFields();
		List<List<String>> lists = readExcelFile(taskCenter);
		//设置每i对应的字段,从第二行开始
		for (int i = skipRowNum; i < lists.size(); i++) {
			Object o = pojoClass.newInstance();
			T t = (T) o;
			List<String> list = lists.get(i); //第i行
			for (int j = 0; j < list.size(); j++) {
				String value = list.get(j);
				for (Field declaredField : declaredFields) {
					RowNum annotation = declaredField.getAnnotation(RowNum.class);
					int num = annotation.num();
					if(j==num){
						declaredField.setAccessible(true);
						if(declaredField.getType() == Integer.class){
							declaredField.set(t, Integer.parseInt(value));
						}else if(declaredField.getType() == BigDecimal.class){
							declaredField.set(t,new BigDecimal(value));
						}else if(declaredField.getType() == Long.class){
							declaredField.set(t,Long.valueOf(value));
						}else{
							declaredField.set(t,value);
						}
					}
				}
			}
			listResult.add(t);
		}
		return listResult;

	}

	protected List<List<String>> readExcelFile(TaskCenter taskCenter) throws Exception{
//		byte[] data =  HttpUtil.downloadBill(taskCenter.getTaskUpUrl());
		byte[] data = null;

		data = File2byte(new File(taskCenter.getTaskUpUrl()));
		if(data == null || data.length < 1) {
			throw new TaskJobException("读取上传文件异常或上传文件为空");
		}
		InputStream is = new ByteArrayInputStream(data);
		List<List<String>> readExcel = ExcelUtil.readExcel(taskCenter.getTaskFileName(), is);
		return readExcel;
	}

	/**
	 * 将文件转换成byte数组
	 * @return
	 */
	public static byte[] File2byte(File tradeFile){
		byte[] buffer = null;
		try
		{
			FileInputStream fis = new FileInputStream(tradeFile);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1)
			{
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e){
			e.printStackTrace();
		}
		return buffer;
	}

}
